/*
 * Wireless Receiver
 *
 * Created: 12/12/2014 6:42:48 p.m.
 * Author: Tick
 */ 

#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#include "nrf24.h"

#define F_CPU = 8000000UL;

//TODO: write_unit_address(uint8_t address)
//Function which stores assigned identifier into EEPROM

//TODO: uint8_t read_unit_address()
//Function which reads stored identifier from EEPROM

//TODO: update_controller()
//Function which updates controller about status change (manual override)

//TODO: Set ALL pins to OUTPUT mode and OFF to save power
//TODO: Disable ADC

int main(void)
{
	nrf24_init();
	nrf24_config(1,1);	//Channel, payload length
	
	//TODO: Accommodate for protocol [address][payload][???]
	uint8_t data[1];	//Create data container for incoming commands
	data[0] = 0;
	
	uint8_t tx_address[5] = {0xD7,0xD7,0xD7,0xD7,0xD7};
	nrf24_tx_address(tx_address);

	uint8_t rx_address[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
	nrf24_rx_address(rx_address);
	
	DDRC |= (1<<7);		//Set message received LED indicator to output mode
	PORTC &= ~(1<<7);	//Turn indicator LED OFF first
	
	DDRD |= (1<<6);		//Set onboard LED to output mode
	DDRD |= (1<<0);		//Set pin D0 as output to control relay
	
	//Perform a double blink to indicate it is working
	PORTD |= (1<<6);
	_delay_ms(500);
	PORTD &= ~(1<<6);
	_delay_ms(500);
	PORTD |= (1<<6);
	_delay_ms(500);
	PORTD &= ~(1<<6);
	//======
	
	
	while(1)
	{
		if(nrf24_dataReady())
		{
			nrf24_getData(data);
			
			PORTC |= (1<<7);
			_delay_ms(500);
			PORTC &= ~(1<<7);
			
			if(data[0]==1)
				PORTD |= (1<<0);
			else
				PORTD &= ~(1<<0);
		}
	}
}